package hoowin.basic.service;

import hoowin.basic.dao.IUserDao;
import hoowin.basic.model.User;

public class UserPool {
	private User[] users;
	private int count;
	private IUserDao userDao;
	
	private UserPool(){
		this.users = new User[1000];
		this.count = 0;
	}
	public void addUser(User user){
		if(this.users[user.getId()%1000]!=null)this.users[user.getId()%1000].destroy();
		this.users[user.getId()%1000] = user;
		count++;
	}
	public void removeUser(User user){
		this.users[user.getId()%1000] = null;
		count--;
	}
	public int getCount(){
		return this.count;
	}
	public User getUserById(int id){
		if(this.users[id%1000]!=null)return this.users[id%1000];
		User user = userDao.getUserById(id);
		if(user!=null){
			user.setUp(this);
			user.setOnline(false);
		}
		return user;
	}
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	public void updateUserWords(String words,int id){
		if(users[id%1000]!=null)users[id%1000].setWords(words);
		userDao.updateWords(words, id);
	}
	public void updateAvatar(String avatar, int id){
		if(users[id%1000]!=null)users[id%1000].setAvatar(avatar);
		userDao.updateAvatar(avatar, id);
	}
}
